﻿using System;
using System.Text;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media.Imaging;
using ComicQ.Controller;
using ComicQ.Domain.Table;

namespace ComicQ.Client.View
{
    /// <summary>
    /// Interaction logic for ComicDetailView.xaml
    /// </summary>
    public partial class ComicDetailView
    {
        private readonly ComicStatusController _comicStatusControl;
        private readonly AuthorController _authorControl;
        private readonly PublisherController _publisherControl;
        private readonly YearController _yearControl;
        private readonly RentController _rentControl;
        private readonly CommentController _commentControl;
        private readonly MemberController _memberControl;
        private readonly ComicController _comicControl;

        private readonly Member _member;
        private readonly Comic _comic;

        public ComicDetailView(Member member, Comic comic)
        {
            InitializeComponent();

            _comicStatusControl = new ComicStatusController();
            _authorControl = new AuthorController();
            _publisherControl = new PublisherController();
            _yearControl = new YearController();
            _rentControl = new RentController();
            _commentControl = new CommentController();
            _memberControl = new MemberController();
            _comicControl = new ComicController();

            _member = member;
            _comic = comic;

            Title = _comic.Title;

            var bmp = new BitmapImage();
            bmp.BeginInit();
            bmp.UriSource = new Uri(@"http://localhost/ComicQImage/" + _comic.Image);
            bmp.EndInit();

            imageViewer.Source = bmp;

            labelTitle.Content = _comic.Title;
            labelAuthor.Content = _authorControl.GetAuthorName(_comic.Author);
            labelPublisher.Content = _publisherControl.GetPublisherName(_comic.Publisher);
            labelYear.Content = _yearControl.GetYearNumber(_comic.Year);
            labelRent.Content = "Rp. " + Decimal.Parse(_rentControl.GetRentRate(_comic.Rent).ToString()).ToString("N");
            labelStatus.Content = _comicStatusControl.GetComicStatusStatus(_comic.ComicStatus);

            labelUsername.Content = _member.Username;

            RefreshComment();
        }

        private void RefreshComment()
        {
            var commentContents = new FlowDocument();
            var comments = _commentControl.GetAllComment();

            foreach (var comment in comments)
            {
                if (_comicControl.GetComic(comment.Comic).Id == _comic.Id)
                {
                    var sb = new StringBuilder();
                    sb.AppendLine(_memberControl.GetMemberName(comment.Member));
                    sb.AppendLine(comment.Date.ToString());
                    sb.AppendLine(comment.Contents);
                    commentContents.Blocks.Add(new Paragraph(new Run(sb.ToString())));
                }
            }

            richTextBoxComment.Document = commentContents;
        }

        private void ButtonCommentClick(object sender, RoutedEventArgs e)
        {
            if (textBoxComment.Text.Equals(""))
            {
                MessageBox.Show("isi komen dulu");
            }
            else if (textBoxComment.Text.Length > 50)
            {
                MessageBox.Show("komen kepanjangan");
            }
            else
            {
                var comment = new Comment
                                  {
                                      Comic = _comic,
                                      Member = _member,
                                      Contents = textBoxComment.Text,
                                      Date = DateTime.Now
                                  };

                _commentControl.AddComment(comment);

                MessageBox.Show("komen sukses ditambahkan");

                RefreshComment();
            }
        }
    }
}
